package net.kesumu.general.android.opengl.object;

import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.*;
import static net.kesumu.general.android.opengl.util.Constants.BYTES_PER_FLOAT;

import java.util.List;

import net.kesumu.general.android.opengl.object.Geometry.Circle;
import net.kesumu.general.android.opengl.object.Geometry.Point;
import net.kesumu.general.android.opengl.object.ObjectBuilder.DrawCommand;
import net.kesumu.general.android.opengl.object.ObjectBuilder.GeneratedData;
import net.kesumu.general.android.opengl.program.TextureShaderProgram;
import net.kesumu.general.android.opengl.util.VertexArray;

public class TextureCircle {
    
    private static final int POSITION_COMPONENT_COUNT = 3;
    private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
    private static final int STRIDE = (POSITION_COMPONENT_COUNT 
        + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT;

    public Point circleCenter;
    public final int texture;
    
    private final VertexArray vertexArray;
    private final List<DrawCommand> drawList;
    
    public TextureCircle(Point center, int texture, int numPoints) {
        this.texture = texture;
        this.circleCenter = center;
        
        Circle circle = new Circle(circleCenter, HISTORY_ICON_WIDTH/2f);
        
        GeneratedData generatedData = ObjectBuilder.createYushabuPhoto(circle, numPoints);
        
        vertexArray = new VertexArray(generatedData.vertexData);
        drawList = generatedData.drawList;
    }
    
    public void bindData(TextureShaderProgram textureProgram) {
        vertexArray.setVertexAttribPointer(
            0, 
            textureProgram.getPositionAttributeLocation(), 
            POSITION_COMPONENT_COUNT,
            STRIDE);
        
        vertexArray.setVertexAttribPointer(
            POSITION_COMPONENT_COUNT, 
            textureProgram.getTextureCoordinatesAttributeLocation(),
            TEXTURE_COORDINATES_COMPONENT_COUNT, 
            STRIDE);
    }
    
    public void draw() {
        for (DrawCommand drawCommand : drawList) {
            drawCommand.draw();
        }
    }

}
